package com.fzb.forum4j.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.ContextLoaderListener;

import com.fzb.forum4j.controller.CommonsAction;
import com.fzb.forum4j.po.Article;
import com.fzb.forum4j.po.Blog;
import com.fzb.forum4j.po.Category;
import com.fzb.forum4j.po.QueryMode;
import com.fzb.forum4j.po.RequestRecord;
import com.fzb.forum4j.service.api.IArticleService;
import com.fzb.forum4j.service.api.IBlogService;
import com.fzb.forum4j.service.api.ICategoryService;
import com.fzb.forum4j.service.api.ITagService;
import com.fzb.forum4j.service.api.IUserService;
import com.fzb.forum4j.service.api.IWebSiteService;

public class WebSiteInitListener extends ContextLoaderListener implements ServletContextListener,HttpSessionListener{


	private IArticleService articleService;
	
	private IBlogService blogService;
	
	private ServletContext context;
	
	private ICategoryService categoryService;
	
	private ITagService tagService;
	
	private IUserService userService;
	
	
	private IWebSiteService service;
	@Override
	public void contextDestroyed(ServletContextEvent sc) {
		
	}

	@Override
	public void contextInitialized(ServletContextEvent sc) {
		
		//初始网站需要加载的数据
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		service=(IWebSiteService) ctx.getBean("webSiteServiceImpl");
		articleService=(IArticleService) ctx.getBean("articleServiceImpl");
		blogService=(IBlogService) ctx.getBean("blogServiceImpl");
		categoryService=(ICategoryService) ctx.getBean("categoryServiceImpl");
		tagService=(ITagService) ctx.getBean("tagServiceImpl");
		userService=(IUserService) ctx.getBean("userServiceImpl");
		context=sc.getServletContext();
		QueryMode<Article> qmd=new QueryMode<Article>();
		Map<String, String> initWeb=service.initWebSite();
		//加载配置文件
		context.setAttribute("webSite", initWeb);
		qmd.setRows(Integer.parseInt(initWeb.get("rows")));
		Set<Category> categories=new LinkedHashSet<Category>();
		Category category=new Category();
		category.setCatalog("news");
		categories.add(category);
		category=new Category();
		category.setCatalog("question");
		categories.add(category);
		qmd.setT(new Article());
		CommonsAction.initDiskUploadPath(sc.getServletContext().getRealPath("/")+"/upload/");
		articleService.getInitData(qmd,categories);
		Set<RequestRecord> requestRecords=new TreeSet<RequestRecord>();
		context.setAttribute("data", qmd.getData());
		
		// 初始统计信息
		Map<String, Set<RequestRecord>> mapAccessRecords=new TreeMap<String, Set<RequestRecord>>();
		SimpleDateFormat sdf=new SimpleDateFormat("MM-dd");
		String d=sdf.format(new Date());
		mapAccessRecords.put(d, requestRecords);
		
		//初始网站主页
		ParseTools.setHome(initWeb.get("home"));
		
		//初始在线人数为0
		context.setAttribute("onlineUserCount", 0);
		context.setAttribute("accessRecords", mapAccessRecords);
		
		//初始推荐Blog
		QueryMode<Blog> qmdBlog=new QueryMode<Blog>();
		Blog blog=new Blog();
		qmdBlog.setT(blog);
		blogService.getBlogRecommend(qmdBlog);
		context.setAttribute("reBlog", qmdBlog.getData());
		
		//初始文章分类信息
		QueryMode<Category> qmdCategory=new QueryMode<Category>();
		Category cg=new Category();
		qmdCategory.setT(cg);
		categoryService.getCategoryTree(qmdCategory);
		context.setAttribute("categorys", qmdCategory.getData().get("categorys"));
		
		//初始化 热门标签
		List<Map<String, String>> tags=tagService.initHotTagByCategoryId(1);
		context.setAttribute("tags", tags);
		
		//初始化会员贡献列表
		 
		context.setAttribute("users", userService.getHotUser());
	}

	@Override
	public void sessionCreated(HttpSessionEvent se) {
		Integer now=(Integer)context.getAttribute("onlineUserCount")+1;
		context.setAttribute("onlineUserCount", now);
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		Integer now=(Integer)context.getAttribute("onlineUserCount")-1;
		context.setAttribute("onlineUserCount", now);
	}

}
